package com.qingcloud.base.interceptor;

import cn.hutool.core.util.StrUtil;
import com.qingcloud.base.constant.SecurityConstants;
import com.qingcloud.base.context.UserContextHolder;
import com.qingcloud.base.remote.pojo.LoginUserDTO;
import com.qingcloud.base.utils.LoginUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 登录用户信息拦截器
 *
 * @author wangjinnan
 */
@Slf4j
@Component
public class LoginUserInfoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) {
        LoginUserDTO loginUserDTO = LoginUtils.getCurrentUser(request, true);
        UserContextHolder.setUserInfo(loginUserDTO);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        //请求完成清除缓存的用户信息
        UserContextHolder.clear();
    }
}
